<?php

    class Admin_NewsController extends Zend_Controller_Action {

        public function init() {
            /* Initialize action controller here */
        }

        public function indexAction() {
            // action body
            $new = new Admin_Model_NewMapper();
            $arr = $new->_getAll();
            $this->view->assign('news', $arr);
        }

        public function addAction() {
            $this->_helper->layout()->disableLayout();
            $table = new Application_Model_DbTable_New();
            $new = new Admin_Model_NewMapper();
            if (isset($_POST['submit'])) {
                $this->_helper->viewRenderer->setNoRender(true);
                $files=isset($_FILES['file_name'])?$_FILES['file_name']:'';
                $url="public/images/admin/new_img";
                $allowType=array('jpg','png','gif','jpeg');
                $size=5120000;
                $file_name=uploadFile($files,$url,$allowType,$size);
                $title = $_POST['title'];
                $content = $_POST['content'];
                $new->_insert($title, $content, $img);
                echo 'OK';
            }
        }

        public function deleteAction() {
            // action body
            $arr = $_GET['chkCustomer'];
            $customer = new Admin_Model_NewMapper();
            foreach ($arr as $id) {
                $customer->_delete($id);
            }
            header("location: /aitishop/admin/news");
        }

        public function updateAction() {
            // action body
            $this->_helper->layout()->disableLayout();
            $new = new Admin_Model_NewMapper();
            if (isset($_POST['submit'])) {
                $this->_helper->viewRenderer->setNoRender(true);
                $id = $_POST['txtId'];
                $title = $_POST['title'];
                $content = $_POST['content'];
                $img = $_POST['img'];
                $new->_update($id, $title, $content, $img);
                echo 'OK';
            } else {
                $id = $_GET['id'];
                $result = $new->_getById($id);
                $this->view->assign('new', $result);
            }
        }

    }
